package com.upcode.weighing.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.upcode.weighing.domain.SysUserRole;
import com.upcode.weighing.mapper.SysUserRoleMapper;
import com.upcode.weighing.service.SysUserRoleService;
import com.upcode.weighing.vo.add.SysUserRoleAddVo;
import com.upcode.weighing.vo.del.BaseDelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author alex
 * @since 2019-04-10
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;


    @Override
    public int addUserRole(SysUserRole sysUserRole) {
        return sysUserRoleMapper.addUserRole(sysUserRole);
    }

    @Override
    public boolean add(SysUserRoleAddVo sysUserRoleAddVo) {
        SysUserRole sysUserRole=new SysUserRole();
        BeanUtils.copyProperties(sysUserRoleAddVo,sysUserRole);
        String roles=sysUserRoleAddVo.getRoleId();
        List<String> list= Arrays.asList(roles.split(","));
        if(null == list || list.isEmpty()){
            throw new RuntimeException("没有选中");
        }
        List<SysUserRole> sysUserRoleList=list.stream().map(item ->{
            SysUserRole sysUserRole1=new SysUserRole();
            sysUserRole1.setId(IdWorker.getId());
            sysUserRole1.setUserId(sysUserRoleAddVo.getUserId());
            sysUserRole1.setRoleId(Long.valueOf(item));
            return sysUserRole1;
        }).collect(Collectors.toList());
        return sysUserRoleMapper.add(sysUserRoleList);
    }

    //删除 解除角色下绑定的用户
    @Override
    public int delRoleUser(BaseDelVo vo) {
        return sysUserRoleMapper.delRoleUser(vo.getIds());
    }

    @Override
    public SysUserRole queryByUserId(Long userId) {
        return sysUserRoleMapper.queryByUserId(userId);
    }

}
